package GUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import DataObjects.Bon;
import DataObjects.ButtonPointOfSale;
import DataObjects.MetaDataOrder;
import DataObjects.Payment;
import Messages.MessageAddNewPaymentOfTable;
import Messages.MessageAddRecipeToDataBase;
import Messages.MessageSavingReply;

public class VKForPointOfSale extends JPanel  implements ActionListener{
	
	private JTextField txt;
	private Color btnColor;
	private Dimension screenSize;
	private int x;
	private int y;
	private JPanel rightPanel;
	private JPanel leftPanel;
	private JPanel leftVisaPanel;
	private MainMenu mainMenu;
	private JTextField txtF=null;
	
	private PfPointOfSale salePoint;
	private Vector<Bon> bon;
	
	private Float totalPrice;
	
	private MetaDataOrder metaDataOrder;
	
	private Vector<Payment> wasPaid;
	
	
	
	public VKForPointOfSale ()
	{
		super();
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
	
        initialize();
    }

	public VKForPointOfSale (Color c,
			                 final PfPointOfSale salePoint,
			                 final Vector<Bon> bon,Float totalPrice,
			                  MainMenu mm ,
			                  MetaDataOrder metaDataOrder,
			                  Vector<Payment> wasPaid)
	{
		super();
		this.mainMenu=mm;
		this.btnColor=c;
		this.salePoint=salePoint;
		this.bon=bon;
		this.totalPrice=totalPrice;
		this.metaDataOrder=metaDataOrder;
		this.wasPaid=wasPaid;
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        initialize();
      
    }
	
	private void initialize()
	{
		this.add(getLeftPanel(salePoint,bon));
		this.add(getRightPanel());	
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		x=screenSize.width;
		y=screenSize.height;
				
        
	}
	
	private JPanel getLeftPanel( final PfPointOfSale salePoint, final Vector<Bon> bon)
	{ 
	    leftPanel = new JPanel();
	    leftPanel.setBackground(new Color(250, 235, 215));
	    leftPanel.setLayout(new GridLayout(4, 3,10,10));
			
	    
        createFiveButton();
        createTenButton();
        createTwentyButton();
        createFiftyButton();
        createOneHundredButton();
        createTwoHundredButton();
        createGoPayButton( bon,"אמריקן");
        createGoPayButton( bon,"ויזה");
        createGoPayButton( bon,"ישרכרט");
        createCancellButton(salePoint,bon);
        createDeleteButton();
        createGoPayButton( bon,"מזומן");
        return leftPanel;
	}
	
	
	
	private void createFiveButton()
    {
		ButtonPointOfSale btn = new ButtonPointOfSale("5");
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        btn.setFocusable(false);
      //  btn.addActionListener(this);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        leftPanel.add(btn);
    }
	
	private void createTenButton()
    {
		ButtonPointOfSale btn = new ButtonPointOfSale("10");
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        btn.setFocusable(false);
     //   btn.addActionListener(this);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        leftPanel.add(btn);
    }
	
	private void createTwentyButton()
    {
		ButtonPointOfSale btn = new ButtonPointOfSale("20");
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        btn.setFocusable(false);
      //  btn.addActionListener(this);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        leftPanel.add(btn);
    }
	
	
	private void createFiftyButton()
    {
		ButtonPointOfSale btn = new ButtonPointOfSale("50");
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        btn.setFocusable(false);
     //   btn.addActionListener(this);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        leftPanel.add(btn);
    }
	
	
	private void createOneHundredButton()
    {
		ButtonPointOfSale btn = new ButtonPointOfSale("100");
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        btn.setFocusable(false);
    //    btn.addActionListener(this);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        leftPanel.add(btn);
    }
	
	private void createTwoHundredButton()
    {
		ButtonPointOfSale btn = new ButtonPointOfSale("200");
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        btn.setFocusable(false);
    //    btn.addActionListener(this);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        leftPanel.add(btn);
    }
	
	
	
	private void createCancellButton(final PfPointOfSale salePoint, final Vector<Bon> bon)
    {
        JButton btn = new JButton("ביטול");
        btn.setBackground(Color.RED);
        btn.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				
     salePoint.remove(salePoint.rightSecondPanel);
     salePoint.add(salePoint.getRightPanel(salePoint, bon));
     salePoint.repaint();
     
			}
		});
        btn.setFocusable(false);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        leftPanel.add(btn);
    }
	
	private void createDeleteButton()
    {
        JButton btn = new JButton("מחק");
        
       
        	btn.setBackground(Color.PINK);
        	btn.addActionListener(new java.awt.event.ActionListener() 
 		   {
 			public void actionPerformed(java.awt.event.ActionEvent e) 
 			{
 				Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
 		        JTextField txtF=(JTextField)component;
 		        int caret=txtF.getCaretPosition();
 		        String text=txtF.getText();	
 		        
 		        txtF.setText(text.substring(0,caret-1) + text.substring(caret));
 		        txtF.setCaretPosition(caret-1);
 			}
 		});
        btn.setFocusable(false);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        leftPanel.add(btn);
    }
	
	private void createGoPayButton(final Vector<Bon> bon,final String type)
    {
        JButton btn = new JButton(type);
        if(type=="מזומן")
        {
        	btn.setBackground(Color.CYAN);
        }
        else
        {
        	btn.setBackground(Color.GREEN);
        }
        btn.setFont(new Font("Tahoma", Font.PLAIN, 20));
        btn.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
 		        JTextField txtF=(JTextField)component;
 		        int caret=txtF.getCaretPosition();
 		        String text=txtF.getText();	
				
				if(text.isEmpty())
					return;
				
		        if(type!="מזומן")
		        {
				
		        	for(int i=0;i<wasPaid.size();i++)
		        	{
						  if(wasPaid.get(i).getTypePayment().equals("מזומן"))
						  {  JOptionPane.showMessageDialog(null,"שגיאה בצורת התשלום");
		        	               return;}
			         }
			        }
				  Date dat = new Date();
				  Payment pay = new Payment();
				 			 
				  DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
				  DateFormat df1 = new SimpleDateFormat("HH:mm:ss");
				 
				  String   data = df.format(dat);
				  String   time = df1.format(dat);
				
				  pay.setDate(data);
				  pay.setTime(time);
				  pay.setTypePayment(type);
					  try{
							
							Integer f=Integer.parseInt(txtF.getText());
													
						}catch(NumberFormatException e1)
						{
							
							JOptionPane.showMessageDialog(null,"סכום לא תקין");
							txtF.setText("");
							 return;	
							
						}
				  
				  pay.setAmount(Float.parseFloat(txtF.getText()));
				  pay.setNumtable(Integer.parseInt(bon.get(0).getTid()));
				  pay.setOrderid(Integer.parseInt(metaDataOrder.getOrderId()));
				  
				  
				  if (Float.parseFloat(txtF.getText())<1)
				    {
			    		JOptionPane.showMessageDialog(null,"סכום לא תקין");
			    		txtF.setText("");
			                return;
			    	}
				  
				  System.out.println("totalPriceVK"+totalPrice);
				  if(Float.parseFloat(txtF.getText())<totalPrice)
				  {
										  
				  	  pay.setChange((float) 0);
				      pay.setTips((float) 0);
				  }
							  
				  else
					  if(type=="מזומן")
					  {
					      pay.setChange(Float.parseFloat(txtF.getText())-totalPrice); 
				          pay.setTips((float) 0);
					  }
					  else
					  {
						  pay.setChange((float) 0);
						  pay.setTips(Float.parseFloat(txtF.getText())-totalPrice);
					  }
				  MessageAddNewPaymentOfTable msgPayment= new MessageAddNewPaymentOfTable(pay);
				 
				  try {
						mainMenu.ms.client.sendToServer(msgPayment);
						MessageSavingReply result = (MessageSavingReply)mainMenu.ms.client.GetMessage();
						 
						//----------Udalyaem panel s klavoy i vozvrachaem panel s viborom zurat tashlum
						salePoint.remove(salePoint.rightSecondPanel);
					    salePoint.add(salePoint.getRightPanel(salePoint, bon));
					    //---------Pereschitivaem i obnovlyaem levuyu panel
						salePoint.calculatePricesForLefrPanel();
						
					 
				    	if (result.getResult()== '0')
				    	{
				    		JOptionPane.showMessageDialog(null,"בעיית תקשורת 03");
				    	}
				    					    	
				    	
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				
			}
		});
        btn.setFocusable(false);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = btn.getFont();
        float size = font.getSize()+x/140;
        btn.setFont(font.deriveFont(size));
        leftPanel.add(btn);
    }
	
	
	
	
	
	private JPanel getRightPanel()
	{ 
	    rightPanel = new JPanel();
	    rightPanel.setBackground(new Color(250, 235, 215));
		rightPanel.setLayout(new GridLayout(4, 3,10,10));
		for(int i = 1; i <= 9; i++) 
        	createButton(Integer.toString(i));
		
		createPointButton();
        createNullButton();
        createNullNullButton();
        return rightPanel;
	}
	
	private void createPointButton()
    {
        JButton btn = new JButton(".");
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        btn.setFocusable(false);
        btn.addActionListener(this);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        rightPanel.add(btn);
    }
	
	
	
    private void createButton(String label)
    {
        JButton btn = new JButton(label);
        if(btnColor!=null)
        {
        	btn.setBackground(btnColor);
        }
        
        btn.setFocusable(false);
        btn.addActionListener(this);
        btn.setFocusPainted(false);
        btn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = btn.getFont();
        float size = font.getSize() + 15.0f;
        btn.setFont(font.deriveFont(size));
        rightPanel.add(btn);
    }
    
    private void createNullButton()
    {
        JButton nullBtn = new JButton("0");
        if(btnColor!=null)
        {
        	nullBtn.setBackground(btnColor);
        }
        nullBtn.setFocusable(false);
        nullBtn.addActionListener(this);
        nullBtn.setFocusPainted(false);
        nullBtn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = nullBtn.getFont();
        float size = font.getSize() + 15.0f;
        nullBtn.setFont(font.deriveFont(size));
        rightPanel.add(nullBtn);
    }
    
    private void createNullNullButton()
    {
        JButton nullBtn = new JButton("00");
        if(btnColor!=null)
        {
        	nullBtn.setBackground(btnColor);
        }
        nullBtn.setFocusable(false);
        nullBtn.addActionListener(this);
        nullBtn.setFocusPainted(false);
        nullBtn.setPreferredSize(new Dimension(x/10, y/8));
        Font font = nullBtn.getFont();
        float size = font.getSize() + 15.0f;
        nullBtn.setFont(font.deriveFont(size));
        rightPanel.add(nullBtn);
    }
    
        
    
    @Override
    public void actionPerformed(ActionEvent e)
    {
    	String actionCommand = e.getActionCommand();
        Component component=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
        txtF=(JTextField)component;
        int caret=txtF.getCaretPosition();
        String text=txtF.getText();
        
                   
        txtF.setText(text.substring(0,caret) +actionCommand+ text.substring(caret));
        txtF.setCaretPosition(caret+txtF.getText().length());
    }
	
    
	
    
    public JTextField getTxt() {
		return txt;
	}

	public void setTxt(JTextField txt) {
		this.txt = txt;
	}
	
	public void setBtnColor(Color c)
	{
		this.btnColor=c;
	}
	
}
